package com.castudio.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castudio.Common.RespPageEntity;
import com.castudio.Entity.Activity;
import com.castudio.Entity.Image;
import com.castudio.Entity.User;
import com.castudio.Mapper.ImageMapper;
import com.castudio.Service.ImageService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小陈同学
 * @since 2023-03-25
 */
@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements ImageService {
    @Autowired
    private ImageMapper imageMapper;
    @Override
    public RespPageEntity getAllImage(Integer page, Integer size, String name) {
        RespPageEntity pageEntity = new RespPageEntity();
        // 默认从0开始
        if (page != null && size != null) {
            page = (page-1)*size;
        }
        // 获取当前也用户信息
        List<Image> image =  imageMapper.getAllImage(page, size, name);
        pageEntity.setData(image);
        Long total = imageMapper.getTotal(name);
        pageEntity.setTotal(total);
        return pageEntity;
    }

    @Override
    public void save(String fileUrl, String name) {
        imageMapper.insert(fileUrl,name);
    }
    @Override
    public IPage<Image> getPage(Integer currentPage, Integer PageSize, Image image) {
        LambdaQueryWrapper<Image> queryWrapper = new LambdaQueryWrapper<Image>();
        queryWrapper.like(Strings.isNotEmpty(image.getName()),Image ::getName, image.getName());
        IPage page = new Page(currentPage,PageSize);
        imageMapper.selectPage(page, queryWrapper);
        return page;
    }
    @Override
    public IPage<Image> getPage(Integer currentPage, Integer pageSize) {
        IPage page = new Page(currentPage,pageSize);
        imageMapper.selectPage(page,null);
        return page;
    }
}
